package com.cskaoyan.user.controller;

import com.cskaoyan.mall.commons.result.ResponseData;
import com.cskaoyan.mall.commons.result.ResponseUtil;
import com.cskaoyan.mall.commons.util.CookieUtil;
import com.cskaoyan.user.constants.UserRetCode;
import com.cskaoyan.user.dal.entitys.Member;
import com.cskaoyan.user.dal.entitys.UserVerify;
import com.cskaoyan.user.dal.persistence.MemberMapper;
import com.cskaoyan.user.dal.persistence.UserVerifyMapper;
import com.cskaoyan.user.dto.KaptchaCodeRequest;
import com.cskaoyan.user.dto.KaptchaCodeResponse;
import com.cskaoyan.user.dto.UserRegisterRequest;
import com.cskaoyan.user.dto.UserRegisterResponse;
import com.cskaoyan.user.service.IKaptchaService;
import com.cskaoyan.user.service.IRegisterService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import tk.mybatis.mapper.entity.Example;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/user")
public class RegisterController {

    @Autowired
    IKaptchaService kaptchaService;
    @Autowired
    IRegisterService registerService;
    @Autowired
    UserVerifyMapper userVerifyMapper;


    @PostMapping("/register")
    public ResponseData register(@RequestBody Map<String,String> map, HttpServletRequest httpServletRequest) {
      String userName = map.get("userName");
      String userPwd = map.get("userPwd");
      String captcha = map.get("captcha");
      String email = map.get("email");
//验证传过来的验证码
      //向用户表中插入一条记录
        //向用户验证表插入记录
        //发送用户激活邮件
        KaptchaCodeRequest kaptchaCodeRequest = new KaptchaCodeRequest();
        String kaptcha_uuid = CookieUtil.getCookieValue(httpServletRequest, "kaptcha_uuid");
kaptchaCodeRequest.setUuid(kaptcha_uuid);
kaptchaCodeRequest.setCode(captcha);


        KaptchaCodeResponse kaptchaCodeResponse = kaptchaService.validateKaptchaCode(kaptchaCodeRequest);


        String code = kaptchaCodeResponse.getCode();


        if (!code.equals(UserRetCode.SUCCESS.getCode())){
            return new ResponseUtil<>().setErrorMsg(kaptchaCodeResponse.getMsg());
        }
        UserRegisterRequest userRegisterRequest = new UserRegisterRequest();
        userRegisterRequest.setEmail(email);
        userRegisterRequest.setUserName(userName);
        userRegisterRequest.setUserPwd(userPwd);


        UserRegisterResponse registerResponse = registerService.register(userRegisterRequest);

        Example example = new Example(UserVerify.class);
        example.createCriteria().andEqualTo("username", userRegisterRequest.getUserName());
        List<UserVerify> userVerifies = userVerifyMapper.selectByExample(example);
        String uuid = userVerifies.get(0).getUuid();
        if(registerResponse.getCode().equals(UserRetCode.SUCCESS.getCode())){


            registerService.sendEmail(uuid, userRegisterRequest);
            return  new ResponseUtil<>().setData(null);


        }
        return  new ResponseUtil<>().setErrorMsg(registerResponse.getMsg());
    }
}
